#include "stdafx.h"
#include "NateBaseDialog.h"
#include <afxpriv.h>

double  g_dblScaleDPIX = 0.0;
double  g_dblScaleDPIY = 0.0;

void InitScalingDPI() 
{
   HDC  hDCScreen = GetDC(0);
   
   g_dblScaleDPIX = GetDeviceCaps(hDCScreen, LOGPIXELSX) / 96.0;
   g_dblScaleDPIY = GetDeviceCaps(hDCScreen, LOGPIXELSY) / 96.0;
   
   ReleaseDC(0, hDCScreen);
}

BOOL CALLBACK EnumChildBaseProc(HWND hWnd, LPARAM lParam)
{
    if(hWnd == NULL || !::IsWindow(hWnd))
    {
        return TRUE;
    }

    //CFont*  pMainFont = GDIOBJECT_MNGR()->GetAppMainFont();
    //::SendMessage(hWnd, WM_SETFONT, (WPARAM)(pMainFont->GetSafeHandle()), TRUE);

    return TRUE;
}

IMPLEMENT_DYNAMIC(CNateBaseDialog, CDialog)

CNateBaseDialog::CNateBaseDialog(LPCTSTR lpszTemplateName, CWnd* pParentWnd)
	: CDialog(lpszTemplateName, pParentWnd)
{
    m_wIDD = 0;

    if((int)g_dblScaleDPIY == 0)
    {
        InitScalingDPI();
    }
}

CNateBaseDialog::CNateBaseDialog(UINT nIDTemplate, CWnd* pParentWnd)
	: CDialog(nIDTemplate, pParentWnd)
{
    m_wIDD = nIDTemplate;

    if((int)g_dblScaleDPIY == 0)
    {
        InitScalingDPI();
    }
}


CNateBaseDialog::~CNateBaseDialog(void)
{
}

INT_PTR CNateBaseDialog::DoModal()
{
	// TODO: Add your specialized code here and/or call the base class
	ASSERT(m_lpszTemplateName != NULL || m_hDialogTemplate != NULL ||
		m_lpDialogTemplate != NULL);

    // load resource as necessary
	LPCDLGTEMPLATE lpDialogTemplate = m_lpDialogTemplate;
	HGLOBAL hDialogTemplate = m_hDialogTemplate;
	HINSTANCE hInst = AfxGetResourceHandle();
	if (m_lpszTemplateName != NULL)
	{
		hInst = AfxFindResourceHandle(m_lpszTemplateName, RT_DIALOG);
		HRSRC hResource = ::FindResource(hInst, m_lpszTemplateName, RT_DIALOG);
		hDialogTemplate = LoadResource(hInst, hResource);
	}
	if (hDialogTemplate != NULL)
		lpDialogTemplate = (LPCDLGTEMPLATE)LockResource(hDialogTemplate);

	// return -1 in case of failure to load the dialog template resource
	if (lpDialogTemplate == NULL)
		return -1;

    // disable parent (before creating dialog)
	HWND hWndParent = PreModal();
	AfxUnhookWindowCreate();
	BOOL bEnableParent = FALSE;
	if (hWndParent && hWndParent != ::GetDesktopWindow() && ::IsWindowEnabled(hWndParent))
	{
		::EnableWindow(hWndParent, FALSE);
		bEnableParent = TRUE;
	}

	TRY
	{
		// create modeless dialog
		AfxHookWindowCreate(this);
		if (CreateDlgIndirect(lpDialogTemplate,
			CWnd::FromHandle(hWndParent), hInst))
		{
			if (m_nFlags & WF_CONTINUEMODAL)
			{
				// enter modal loop
				DWORD dwFlags = MLF_SHOWONIDLE;
				if (GetStyle() & DS_NOIDLEMSG)
					dwFlags |= MLF_NOIDLEMSG;
				VERIFY(RunModalLoop(dwFlags) == m_nModalResult);
			}

			// hide the window before enabling the parent, etc.
			if (m_hWnd != NULL)
				SetWindowPos(NULL, 0, 0, 0, 0, SWP_HIDEWINDOW|
				SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOZORDER);
		}
	}
	CATCH_ALL(e)
	{
		if ( e ) e->Delete();
		m_nModalResult = -1;
	}
	END_CATCH_ALL

		if (bEnableParent)
			::EnableWindow(hWndParent, TRUE);
	if (hWndParent != NULL && ::GetActiveWindow() == m_hWnd)
		::SetActiveWindow(hWndParent);

	// destroy modal window
	DestroyWindow();
	PostModal();

	// unlock/free resources as necessary
	if (m_lpszTemplateName != NULL || m_hDialogTemplate != NULL)
		UnlockResource(hDialogTemplate);
	if (m_lpszTemplateName != NULL)
		FreeResource(hDialogTemplate);

	return m_nModalResult;
}

BOOL CNateBaseDialog::OnInitDialog()
{
	CDialog::OnInitDialog();

    if((int)g_dblScaleDPIY != 1)
    {
        m_setDPI.Attach(AfxFindResourceHandle(IMAKEINTRESOURCE(m_wIDD), RT_DIALOG),
                        m_hWnd, (int)m_wIDD, 96.0); // 96 is the DPI
    }

    return TRUE;
}